<?php if (! defined ( 'BASEPATH' )) exit ( 'No direct script access allowed' );

class MY_Model extends CI_Model {
    var $table_name;

    function __construct($tn = "") {
        parent::__construct();
        $this->table_name = $tn;
    }

    /**
     * 按照ID得到数据
     * @param $id
     * @return 得到的数据数组
     */
    function get_by_id($id, $select_key = '*') {
		$query = $this->db->query("SELECT " . $select_key . " FROM {$this->table_name} WHERE id = ?", array($id));
		$data = $query->row_array();
		if (count($data) == 1) {
			return $data[$select_key];
		} else {
			return $data;
		}
    }
    
	/**
     * 按照外键id得到数据（针对各分类用户表：a_admin, a_employee, s_society, s_teacher）
     * @param $foreign_id
     * @param $select_key
     */
    function _get_by_foreign_id($foreign_id, $select_key = '*') {
    	//if ($this->table_name != 'a_admin' OR $this->table_name != 'a_employee' OR $this->table_name != 's_society' OR $this->table_name != 's_teacher') { return FALSE; }
    	$tn_array = explode('_', $this->table_name);
    	$foreign_id_name = $tn_array[1] . '_id';
    	$query = $this->db->query("SELECT " . $select_key . " FROM {$this->table_name} WHERE " . $foreign_id_name . " = ?", array($foreign_id));
		$data = $query->row_array();
		if (count($data) == 1) {
			return $data[$select_key];
		} else {
			return $data;
		}
    }
    
	/**
     * 按照指定键名得到数据
     * @param $specified_key_name
     * @param $specified_key_value
     * @param $select_key
     * @param boolean $row_str_FLAG
     * @return 得到的数据数组/单条数据
     */
    function _get_by_specified_key($specified_key_name, $specified_key_value, $select_key = '*', $row_str_FLAG = FALSE) {
		$query = $this->db->query("SELECT {$select_key} FROM {$this->table_name} WHERE {$specified_key_name} = ?", array($specified_key_value));
		switch ($row_str_FLAG) {
			case TRUE:
				$data = $query->row_array();
				return $data[$select_key];
				break;
			case FALSE:
				$data = $query->result_array();
				
				if (!stripos($select_key, ',') && $select_key != '*') {
					if (!empty($data)) {
						foreach ($data as $v) {
							$temp[] = $v[$select_key];
						}
						return $temp;
					} else {
						return NULL;
					}
				} else {
					return $data;
				}
				break;
		}
    }

    /**
     * 得到全部数据
     * @return 得到所有的数据的对象数组
     */
    function get_all() {
        $query = $this->db->get($this->table_name);
        return $query->result_array();
    }

    /**
     * 得到数据总量
     * @return 数据量的数
     *
     */
    function get_count() {
        return $this->db->count_all_results($this->table_name);
    }

    /**
     * 按照ID删除数据
     * @param $id
     */
    function delete($id) {
        $this->db->where('id', $id);
        $this->db->delete($this->table_name);
    }
    
    /**
     * 按照外键id删除数据（针对各分类用户表：a_admin, a_employee, s_society, s_teacher）
     * @param $foreign_id
     */
    function delete_by_foreign_id($foreign_id) {
    	$tn_array = explode('_', $this->table_name);
    	$foreign_id_name = $tn_array[1] . '_id';
    	$this->db->where($foreign_id_name, $foreign_id);
        $this->db->delete($this->table_name);
    }
    
    /**
     * 
     * @param $specified_key_name
     * @param $specified_key_value
     */
    function _delete_by_specified_key($specified_key_name, $specified_key_value) {
    	$this->db->where($specified_key_name, $specified_key_value);
    	$this->db->delete($this->table_name);
    }

    /**
     * 删除指定ID数据
     * @param $ids
     */
    function delete_by_ids($ids) {
        foreach($ids as $v) {
            $this->db->where('id', $v);
            $this->db->delete($this->table_name);
        }
    }

    /**
     * 新增数据
     * @param $data
     */
    function save($data) {
        $this->db->insert($this->table_name, $data);
        return $this->db->insert_id();
    }

    /**
     * 修改数据
     * @param $data
     */
    function update($data) {
        $this->db->where('id', $data['id']);
        $this->db->update($this->table_name, $data);
    }
    
	/**
     * 按照外键id修改数据（针对各分类用户表：a_admin, a_employee, s_society, s_teacher）
     * @param $foreign_id
     */
    function update_by_foreign_id($foreign_id, $data) {
    	$tn_array = explode('_', $this->table_name);
    	$foreign_id_name = $tn_array[1] . '_id';
    	$this->db->where($foreign_id_name, $foreign_id);
        $this->db->update($this->table_name, $data);
    }
    
    function _update_by_specified_key($specified_key_name, $specified_key_value, $data) {
    	$this->db->where($specified_key_name, $specified_key_value);
        $this->db->update($this->table_name, $data);
    }
    
    /**
     * 得到表中所有的字段名称 字段名称为Key 值 为空
     */
    function get_fields() {
        $fields = $this->db->list_fields($this->table_name);
        $form_data = array();
        foreach ($fields as $v) {
            $form_data[$v] = "";
        }
        return $form_data;
    }
    
	/**
     * 得到 表的字段
     */
    function get_table_fields() {
        $fields = $this->db->list_fields($this->table_name);
        return $fields;
    }

}

